package resources;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.RDFWriter;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.ArrayList;

import config.connect;
import config.database;;

public class SemanticDictionary {

	ArrayList<Monier> wordlist=null;
    Model m;

  
    public SemanticDictionary(ArrayList<Monier> dict){
    	m= ModelFactory.createDefaultModel();
    	this.wordlist= dict;
    }
    public void close(){
    	
    	m.close();
    	
    }
    
public void createRDFNodes(){
	
     System.out.println("wordlist size= "+wordlist.size());
     for(int i=0;i<wordlist.size();i++){
         Resource r=m.createResource("http://anvesh.com/word/"+wordlist.get(i).getKey1());
         Property meaning=m.createProperty("http://anvesh.com/meaning");
        // Property pos=m.createProperty("http://anvesh.com/pos");
         r.addProperty(meaning, wordlist.get(i).getC());
        // r.addProperty(pos,wordlist.get(i).get)
     }
}

public void printFromModel(){
	
	StmtIterator iter = m.listStatements();

	// print out the predicate, subject and object of each statement
	while (iter.hasNext()) {
	    Statement stmt      = iter.nextStatement();  // get next statement
	    Resource  subject   = stmt.getSubject();     // get the subject
	    Property  predicate = stmt.getPredicate();   // get the predicate
	    RDFNode   object    = stmt.getObject();      // get the object

	    System.out.print(subject.toString());
	    System.out.print(" " + predicate.toString() + " ");
	    if (object instanceof Resource) {
	       System.out.print(object.toString());
	    } else {
	        // object is a literal
	        System.out.print(" \"" + object.toString() + "\"");
	    }

	    System.out.println(" .");
	} 
}

public void writeXML(){
	
	try {
		RDFWriter writer= m.getWriter();
		File file_name= new File("sanskrit_xml.rdf");
		file_name.delete();
		OutputStream fout= new FileOutputStream (file_name);
		writer.write(m, fout, "http://dunno//");
		fout.flush();
		fout.close();
	} 
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

}
